package com.choichumgming.rentcat.service.Impl;

import com.choichumgming.rentcat.mapper.PmsCommentMapper;
import com.choichumgming.rentcat.model.PmsComment;
import com.choichumgming.rentcat.model.PmsCommentExample;
import com.choichumgming.rentcat.service.OmsOrderFeignApi;
import com.choichumgming.rentcat.service.PmsCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class PmsCommentServiceImpl implements PmsCommentService {
    @Autowired
    private PmsCommentMapper pmsCommentMapper;

    @Resource
    private OmsOrderFeignApi omsOrderFeignApi;

    /**
     * @Description : 添加评论
     *
     * @param comment
     * @Return : int
     * @Author : choichumgming
     * @Date : 27/2/2020 23:52
     */
    @Override
    public int add(PmsComment comment) {
        log.warn(comment.getOrderId()+"");
        omsOrderFeignApi.updateOrderStatus(comment.getOrderId(),4);
        return pmsCommentMapper.insert(comment);
    }

    /**
     * @Description : 删除评论
     *
     * @param commentId
     * @Return : int
     * @Author : choichumgming
     * @Date : 27/2/2020 23:53
     */
    @Override
    public int delete(Long commentId) {
        PmsCommentExample example = new PmsCommentExample();
        example.createCriteria().andCommentIdEqualTo(commentId);
        return pmsCommentMapper.deleteByExample(example);
    }

    /**
     * @Description : 修改评论
     *
     * @param commentId
     * @param comment
     * @Return : int
     * @Author : choichumgming
     * @Date : 27/2/2020 23:53
     */

    @Override
    public int update(Long commentId, PmsComment comment) {
        PmsCommentExample example = new PmsCommentExample();
        example.createCriteria().andCommentIdEqualTo(commentId);
        return pmsCommentMapper.updateByExampleSelective(comment,example);
    }

    /**
     * @Description : 获得指定用户所有评论
     *
     * @param userId
     * @Return : java.util.List<com.choichumgming.rentcat.model.PmsComment>
     * @Author : choichumgming
     * @Date : 27/2/2020 23:53
     */
    @Override
    public List<PmsComment> getUserCommentlist(Long userId) {
        PmsCommentExample example = new PmsCommentExample();
        example.createCriteria().andUserIdEqualTo(userId);
        return pmsCommentMapper.selectByExample(example);
    }

    /**
     * @Description : 获得指定商品的所有
     *
     * @param productId
     * @Return : com.choichumgming.rentcat.model.PmsComment
     * @Author : choichumgming
     * @Date : 1/3/2020 15:38
     */
    @Override
    public List<PmsComment> getProdcutComments(Long productId) {
        PmsCommentExample example = new PmsCommentExample();
        example.createCriteria().andProductIdEqualTo(productId);
        List<PmsComment> commentList = pmsCommentMapper.selectByExample(example);
        return commentList;
    }

    /**
     * @Description : 获取评论详情
     *
     * @param commentId
     * @Return : com.choichumgming.rentcat.model.PmsComment
     * @Author : choichumgming
     * @Date : 27/2/2020 23:53
     */
    @Override
    public PmsComment getItem(Long commentId) {
        PmsCommentExample example = new PmsCommentExample();
        example.createCriteria().andCommentIdEqualTo(commentId);
        List<PmsComment> commentList = pmsCommentMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(commentList)){
            return commentList.get(0);
        }
        return null;
    }



}
